﻿using System;
using System.Design;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design.Behavior;
using System.ComponentModel.Design.Serialization;

// TODO: Move whole InfFront.InfoSources.Base.Design to a separate assembly

namespace InfFront.InfoSources.Base.Design
{
    public class InfoSourceDesigner: BaseRootDesigner
    {
        public InfoSourceDesigner()
        {
        }

        protected override BaseDesignerControl CreateControl()
        {
            var result = new InfoSourceDesignerControl();
            result.Designer = this;
            return result;
        }

        public InfoSource Source
        {
            get { return (InfoSource)Component; }
        }
    }

    public class InfoSourceRootCodeDomSerializer : BaseRootCodeDomSerializer
    {
        public override System.CodeDom.CodeTypeDeclaration Serialize(IDesignerSerializationManager manager, object root, System.Collections.ICollection members)
        {
            // ordering the list so fields and commands are serialized *before* their owners
            var orderedMembers = new System.Collections.ArrayList(members);
            for (int i = 1; i < orderedMembers.Count; ++i)
            {
                var asInfoSource = orderedMembers[i] as InfoSource;
                if (asInfoSource == null)
                    continue;
                int j = i + 1;
                for (; j < orderedMembers.Count; ++j)
                {
                    var asInfoField = orderedMembers[j] as InfoField;
                    if (asInfoField != null)
                    {
                        if (asInfoSource.Fields.Contains(asInfoField))
                            break;
                        continue;
                    }
                    var asInfoCommand = orderedMembers[j] as InfoCommand;
                    if (asInfoCommand != null)
                    {
                        if (asInfoSource.Commands.Contains(asInfoCommand))
                            break;
                        continue;
                    }
                    var asChildInfoSource = orderedMembers[j] as InfoSource;
                    if (asChildInfoSource != null)
                    {
                        if (asChildInfoSource.IsChildOf(asInfoSource))
                            break;
                        continue;
                    }
                }
                if (j != orderedMembers.Count)
                {
                    orderedMembers.RemoveAt(i);
                    orderedMembers.Insert(j, asInfoSource);
                    --i;
                }
            }

            return base.Serialize(manager, root, orderedMembers);
        }
    }

    public class InfoSourceCodeDomSerializer : CodeDomSerializer
    {
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            return base.Serialize(manager, value);
        }
    }
}